// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-16 17:03
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class SummaryRanges {
    /**
     * 记录数组中的最小数据区间
     */
    public List<String> summaryRanges(int[] nums) {
        int n = nums.length;
        ArrayList<String> res = new ArrayList<>();
        if (n == 0) {
            return res;
        }
        if (n == 1) {
            res.add(nums[0] + "");
            return res;
        }
        int pre, cur = 0;
        for (int i = 1; i < n; i++) {
            pre = cur;
            while ((i < n) && (nums[i] == (nums[i - 1] + 1))) {
                ++i;
            }
            if (i < n) {
                cur = i;
            } else {
                cur = i - 1;
            }

            if (i - pre <= 1) {
                res.add(nums[pre] + "");
            } else {
                res.add(nums[pre] + "->" + nums[i - 1]);
            }
        }
        if (nums[n - 1] != (nums[n - 2] + 1)) {
            res.add(nums[n - 1] + "");
        }
        return res;
    }

    @Test
    public void shout() {
        //  int[] nums = {0, 1, 2, 4, 5, 7};
        int[] nums = {-1, 1};
        //int[] nums = {0, 2, 3, 4, 6, 8, 9};
        System.out.println(summaryRanges(nums));
    }

}
